package taskOnClasses;

//import Citizen;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Company {

    private String CompanyName;
    private String CompanyAdress;
    private List<Client> Clients;

    private class AlreadyContains extends Exception {

        @Override
        public String toString() {
            return "Such client already exists in databases";
        }
    }

    private class NoSuchClient extends Exception {

        @Override
        public String toString() {
            return "No such client was found in databases";
        }
    }

    private class AccountIsFrozen extends Exception {

        @Override
        public String toString() {
            return "Account is frozen";
        }
    }

    public Company(String someName, String someAdress) {
        this.CompanyName = someName;
        this.CompanyAdress = someAdress;
        this.Clients = new LinkedList<Client>();
    }

    public void addClient(Client newClient) throws AlreadyContains {
    	if (!Clients.contains(newClient)){
    		Clients.add(newClient);
    	}
    	else throw new AlreadyContains();
    }

    public boolean containsClient(String someSurname, int someID)
    throws NoSuchClient{
        boolean found = false;
        for (Client c : Clients) {
            if ((c.getSurname().equals(someSurname)) && (c.getID() == someID)) {
                found = true;
            }
        }
        if (!found) throw new NoSuchClient();
        return found;
    }

    public Client findClient(String someSurname, int someID) {
        Client someC = null;

        for (Client c : Clients) {
            if ((c.getSurname().equals(someSurname)) && (c.getID() == someID)) {
                someC = c;
            }
        }
        return someC;
    }

    public void removeClient(String Surname, int ID) throws NoSuchClient {
        boolean wasFound = false;
        for (Iterator<Client> currIter = Clients.iterator(); currIter.hasNext();) {
            Client temp = currIter.next();
            if (temp.getSurname().equals(Surname)
                    && (temp.getID() == ID)) {
                wasFound = true;
                currIter.remove();
                break;
            }
        }
        if (!wasFound) {
            throw new NoSuchClient();
        }
    }

    public void depositForClient(String someSurname, int someID, double Ammount)
            throws AccountIsFrozen {
        for (Client c : Clients) {
            try {
                c.topUpAccount(Ammount);
            } catch (Exception e) {
                throw new AccountIsFrozen();
            }
        }
    }

    public void withdrawForClient(String someSurname, int someID, double Ammount)
            throws AccountIsFrozen {
        for (Client c : Clients) {
            try {
                c.withdraw(Ammount);
            } catch (Exception e) {
                throw new AccountIsFrozen();
            }
        }
    }
    public void changeClientData(String oldSurname, int oldID, Client changedClient) throws NoSuchClient {
        boolean wasFound = false;
        for (Client c : Clients) {
            if ((c.getSurname().equals(oldSurname)) && (c.getID() == oldID)) {
                wasFound = true;
                c.setIsFrozen(changedClient.isFrozen());
                c.setSurname(changedClient.getSurname());
                c.setNewID(changedClient.getID());
                c.setAge(changedClient.getAge());
                c.setNewPercentage(changedClient.getPercentage()); 
            }
        }
        if (!wasFound) {
            throw new NoSuchClient();
        }
    }

    public void generateReport() throws FileNotFoundException, IOException {
        try {
            Date today = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("y_MM_dd");
            String outDate = formatter.format(today);

            File outFile = new File(CompanyName + " clients report " + outDate + ".txt");
            BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));

            writer.append(today.toString());
            writer.newLine();
            writer.newLine();

            for (Client c : Clients) {
                writer.append(c.toString());
                writer.newLine();
            }
            writer.newLine();

            writer.close();
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException();
        } catch (IOException e) {
            throw new IOException();
        }
    }
}
